/**
 * NodeDescriptorTest.java
 * ************************** 
 * @date Mar 18, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package JUnitTests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import gossipServices.basic.nodeDescriptors.ConcreteNodeDescriptor;
import gossipServices.basic.nodeDescriptors.GeneralNodeDescriptor;
import gossipServices.basic.nodeDescriptors.NodeDescriptor;
import gossipServices.basic.nodeDescriptors.PssNodeDescriptor;
import gossipServices.basic.nodeDescriptors.comparators.AgeSorterer;
import gossipServices.basic.nodeDescriptors.comparators.IdSorterer;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import org.junit.Before;
import org.junit.Test;

/**
 * @author michele
 * 
 * 
 * Description: 
 *
 */
public class NodeDescriptorTest {
	
	PssNodeDescriptor cnd1, cnd2, cnd3;

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	@Before
	public void setUp() throws Exception {
		try {
			InetAddress add = InetAddress.getLocalHost();
			cnd1 = new ConcreteNodeDescriptor("cnd1", add, 0);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		cnd2 = new ConcreteNodeDescriptor("Bubba");
		cnd3 = new ConcreteNodeDescriptor("cnd3");
	}

	/**
	 * Test method for {@link gossipServices.basic.nodeDescriptors.GeneralNodeDescriptor#ConcreteNodeDescriptor(java.lang.String, java.net.InetAddress, int)}.
	 */
	@Test
	public void testConcreteNodeDescriptorStringInetAddressInt() {
		
		
		assertNotNull(cnd1);
//		fail("Not yet implemented");
	}

	/**
	 * Test method for {@link gossipServices.basic.nodeDescriptors.GeneralNodeDescriptor#ConcreteNodeDescriptor(java.lang.String)}.
	 */
	@Test
	public void testConcreteNodeDescriptorString() {
		
		
		assertNotNull(cnd2);
		assertNotNull(cnd3);
		
//		fail("Not yet implemented");
	}

	/**
	 * Test method for {@link gossipServices.basic.nodeDescriptors.GeneralNodeDescriptor#getNodeAddress()}.
	 */
	@Test
	public void testGetNodeAddress() {
		try {
			assertEquals(InetAddress.getLocalHost(), cnd1.getNodeAddress());
			System.out.println(cnd1.getNodeName()+" local address is: "+cnd1.getNodeAddress());
			
			assertTrue(!InetAddress.getLocalHost().equals(cnd2.getNodeAddress()));
			System.out.println(cnd2.getNodeName()+" local address is: "+cnd2.getNodeAddress());
			
			NodeDescriptor newNode = new GeneralNodeDescriptor("newNode");
			assertEquals(InetAddress.getLocalHost(), newNode.getNodeAddress());
			System.out.println(newNode.getNodeName()+" local address is: "+newNode.getNodeAddress());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
//		fail("Not yet implemented");
	}

	/**
	 * Test method for {@link gossipServices.basic.nodeDescriptors.GeneralNodeDescriptor#getNodeName()}.
	 */
	@Test
	public void testGetNodeName() {
		String names[] = {"Bubba", "cnd1", "cnd3"};
		String ver[] = {};
		for(int i=0; i<names.length; i++)
			names[i] = ConcreteNodeDescriptor.adjustNodeName(names[i]);
		assertEquals(names[0],cnd2.getNodeName());
		
		assertEquals(names[1],cnd1.getNodeName());
		assertEquals(names[2],cnd3.getNodeName());
//		fail("Not yet implemented");
	}

	/**
	 * Test method for {@link gossipServices.basic.nodeDescriptors.GeneralNodeDescriptor#getAge()}.
	 */
	@Test
	public void testGetAge() {
		assertEquals(0, cnd1.getAge().intValue());
		assertEquals(0, cnd2.getAge().intValue());
//		fail("Not yet implemented");
	}
	
	/**
	 * Test method for {@link gossipServices.basic.nodeDescriptors.GeneralNodeDescriptor#increaseAge()}.
	 */
	@Test
	public void testIncreaseAge(){
		for(int i=0 ; i<30 ; i++){
			cnd1.increaseAge();
			if(i%2 == 0)
				cnd2.increaseAge();
			
		}
		assertEquals(30, cnd1.getAge().intValue());
		assertEquals(15, cnd2.getAge().intValue());
		System.out.println("Age of "+cnd1.getNodeName()+" is "+cnd1.getAge());
		System.out.println("Age of "+cnd2.getNodeName()+" is "+cnd2.getAge());
	}
	
	/**
	 * Test method for {@link gossipServices.basic.nodeDescriptors.GeneralNodeDescriptor#compareTo()}.
	 */
	@Test
	public void compareToTest(){
//		int a = cnd1.compareTo(cnd2);
//		int b = cnd1.compareTo(cnd3);
//		int c = cnd2.compareTo(cnd3);
//		int d = cnd3.compareTo(cnd1);
		Comparator<NodeDescriptor> sorterer = new AgeSorterer();
		int a = sorterer.compare(cnd1, cnd2) ;
		int b = sorterer.compare(cnd1, cnd3) ;
		int c = sorterer.compare(cnd2, cnd3) ;
		int d = sorterer.compare(cnd3, cnd1) ;
		assertTrue(a == 0);
		assertTrue(b == 0);
		assertTrue(d == 0);
		
		//transitive compare test:
		assertTrue(c == 0);
	}
	
	@Test
	public void generalNodeDescriptorCompareToTest(){
//		int size = 8;
//		NodeDescriptor descriptors[] = new NodeDescriptor[size];
		String names[] = {"gen1", "genRosso", "Gustavo", "Sondrio",
				"pallino", "bubba", "gen2", "Rodrigo", "gen3", "1"};
		Vector<NodeDescriptor> vec = new Vector<NodeDescriptor>();
		System.out.println("GeneralNodeDescriptorTest");
		for(int i=0; i<names.length; i++){
			vec.add(new GeneralNodeDescriptor(names[i]));
			System.out.println(i+") "+vec.get(i));
		}
		Collections.sort(vec, new IdSorterer());
		int prec=0;
		System.out.println("After sorting:\n"
				+prec+") "+vec.get(prec));
		for(int i=1; i<vec.size(); i++){
			assertTrue(vec.get(prec).getNodeID() < vec.get(i).getNodeID());
			System.out.println(i+") "+vec.get(i));
			prec = i;
		}
	}

}
